
(function() {

(function () {
	chrome.storage.local.get('settings', function (args) {
		if (args.settings) {
			if (args.settings.stopAutoPlay) {
				unplayVideo();
			}
		} else {
			init();
			unplayVideo();
		}
	})
})();

function unplayVideo() {
	var func = function() {
		if (typeof window.ArticleVideoTv == "undefined")
			return;
		// instead of replacing the player element, replace the function that starts the player
		window.ArticleVideoTv.autoPlayVideo = function(playerId) {
			console.info('no more autoplay for ' + playerId + '!');
			var div = document.createElement('div');
//			div.innerHtml = "'Quiet, Ynet. QuieNet.'";	// doesn't work, for some reason
			div.textContent = 'AutoPlay prevented by QuieNet. Quiet, Ynet!';
			div.id = 'quienet' + playerId;
			div.setAttribute('style', 'z-index:100;position:relative;line-height:1em;margin-top:-3em;direction:ltr;text-align:left;color:white;background:rgba(0,100,100,0.5)');
			document.getElementById('fpContainer' + playerId).appendChild(div);
		};
	};
	// the replacement must be done before $.ready, otherwise it will be too late.
	// this is a problem, because content scripts that use run_at = document_end
	// are run after the ready hander.
	// so, this content script is run at document_start, and injects code that runs
	// after the DOM is build (hence ArticleVideoTv created) but before the ready event.
	document.addEventListener('DOMContentLoaded', function(){
		var script = document.createElement("script");
		script.type="text/javascript";
		script.textContent = '(' + func + ')()';
		document.head.appendChild(script);
	});

}

function init() {
	chrome.storage.local.set({'settings': {'stopAutoPlay': true}}, function (){});
}

})();

// live streaming: http://www.ynet.co.il/articles/0,7340,L-4546067,00.html
// multiple players: http://www.ynet.co.il/articles/0,7340,L-4546435,00.html
// no autoplay: http://www.ynet.co.il/articles/0,7340,L-4546304,00.html
// non-first autoplayer: http://www.ynet.co.il/articles/0,7340,L-4545001,00.html